package com.tjb.dao;

import com.tjb.entity.MessageBeam;
import com.tjb.util.DBCon;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MessageDaoImpl implements MessageDao{
    private Connection conn = DBCon.getConnection();
    private PreparedStatement psd = null;
    private ResultSet rs = null;
    @Override
    public List<MessageBeam> getMessages() {
        String sql = "select * from MessageInfo";
        List<MessageBeam> list = new ArrayList<MessageBeam>();
        try {
            psd = conn.prepareStatement(sql);
            rs = psd.executeQuery();
            while(rs.next()){
                MessageBeam mb = new MessageBeam();
                mb.setMid(rs.getInt("mid"));
                mb.setMocntent(rs.getString("mcontent"));
                mb.setPublish(rs.getDate("publish"));
                list.add(mb);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int delete(int[] mid) {
        int count = 0;
        for(int id : mid){
            try {
                String sql = "delete from MessageInfo where mid = ?";
                psd = conn.prepareStatement(sql);
                psd.setInt(1,id);
                psd.executeUpdate();
                count ++;
            } catch (SQLException throwables) {
                System.out.println("删除错误"+throwables.getMessage());
            }
        }
        return count;
    }

    public static void main(String[] args) {
        MessageDao dao = new MessageDaoImpl();
        List<MessageBeam> lisss = dao.getMessages();
        System.out.println(lisss);
    }
}
